package com.wlgzs.booksystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlgzs.booksystem.config.jwt.JWTUtil;
import com.wlgzs.booksystem.entity.User;
import com.wlgzs.booksystem.enums.Result;
import com.wlgzs.booksystem.enums.ResultCodeEnum;
import com.wlgzs.booksystem.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.parser.Token;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wlgzs-sjl
 * @date 2020/11/17 16:42
 */
@RestController
@RequestMapping("/login")
@Api(tags = "登录管理")
public class LoginController {
    @Autowired
    UserService userService;


    @PostMapping("/login")
    @ApiOperation(value = "登录", httpMethod = "POST", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "用户手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String"),
    })
    public Result login(String phone, String password) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("phone", phone);
        User user = userService.getOne(wrapper);
        if (user == null) {
            return new Result(0, "用户不存在");
        } else if (!user.getPassword().equals(password)) {
            return new Result(0, "密码错误");
        } else {
            List<Object> list = new ArrayList<>();
            String token = JWTUtil.createToken(phone);
            list.add(token);
            list.add(user);
            return new Result(ResultCodeEnum.SUCCESS, list);
        }
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册", httpMethod = "POST", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "用户手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String"),
    })
    public Result register(String phone, String password) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("phone",phone);
        if (userService.getOne(wrapper)!=null){
            return new Result(0,"该手机号已存在");
        }
        User user=new User();
        user.setPhone(phone);
        user.setPassword(password);
        userService.save(user);
        return new Result(ResultCodeEnum.SUCCESS);
    }


    @PostMapping("/logout")
    @ApiOperation(value = "退出登录", httpMethod = "POST", response = Result.class)
    public Result logout() {
        return new Result(ResultCodeEnum.SUCCESS);
    }
}
